package com.qsmx.store.app.main.mem.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.qsmx.store.app.R;
import com.qsmx.store.app.main.mem.entity.LeftTitleEntity;
import com.qsmx.store.app.main.mem.holder.MemCardLeftHolder;

import java.util.List;

public class LeftAdapter extends RecyclerView.Adapter<MemCardLeftHolder> {

    private Context mContext;
    private List<LeftTitleEntity.LeftTitleList> mData;


    //定义接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //声明自定义的监听接口
    private LeftAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(LeftAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public LeftAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<LeftTitleEntity.LeftTitleList> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public MemCardLeftHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        MemCardLeftHolder holder = new MemCardLeftHolder(LayoutInflater.from(
                mContext).inflate(R.layout.adapter_mem_card_left, viewGroup,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MemCardLeftHolder memCardLeftHolder, final int position) {

        if (mData.get(position).isSelect()) {
            memCardLeftHolder.linContent.setBackgroundColor(mContext.getResources().getColor(R.color.theme_yellow_color));
        } else {
            memCardLeftHolder.linContent.setBackgroundColor(mContext.getResources().getColor(R.color.theme_black));
        }

        memCardLeftHolder.tvTitle.setText(mData.get(position).getTitle());
        Glide.with(mContext).load(mData.get(position).getIcon()).into(memCardLeftHolder.ivIcon);

        memCardLeftHolder.linContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0) {
            return mData.size();
        } else {
            return 0;
        }
    }
}
